package com.elh.serurity.exceptions;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liulei
 */
@ControllerAdvice
public class CustomException {

    @ExceptionHandler(value = VerificationExcepton.class)
    @ResponseBody
    public Map<String,Object> handlerException(VerificationExcepton e) {
        Map<String,Object> resMap = new HashMap<>();
        resMap.put("code","500");
        resMap.put("data",e.getMessage());
        return resMap;
    }

    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    public Map<String,Object> handleAccessDeniedException(AccessDeniedException e)
    {
        Map<String,Object> resMap = new HashMap<>();
        resMap.put("code","500");
        resMap.put("data",e.getMessage());
        return resMap;
    }

    @ExceptionHandler(AuthenticationException.class)
    @ResponseBody
    public Map<String,Object> handleAuthorizationException(AuthenticationException e)
    {
        Map<String,Object> resMap = new HashMap<>();
        resMap.put("code","500");
        resMap.put("data",e.getMessage());
        return resMap;
    }

}
